package com.study.spark.ml.movie.basic

import java.awt.Font

import com.study.spark.ml.movie.Util
import org.jfree.chart.axis.CategoryLabelPositions
import scalax.chart.module.ChartFactories

import scala.collection.immutable
import scala.collection.immutable.ListMap

/**
  * 评分分布柱状图
  *
  * @author: stephen.shen
  * @create: 2019-04-10 10:54
  */
object RatingCountChart {

  def main(args: Array[String]): Unit = {
    val ratingCount:Array[(Int,Long)] = RatingData.getRatingCount
    val ds = new org.jfree.data.category.DefaultCategoryDataset

    // 方式一
    for( x <- 0 until ratingCount.length) {
      ds.addValue(ratingCount(x)._2,"Rating Values", ratingCount(x)._1)
    }

    // 方式二
    //val sorted =  ListMap(ratingCount.toSeq.sortBy(_._1):_*)
    //sorted.foreach{ case (k,v) => ds.addValue(v,"Rating Values", k)}

    val chart = ChartFactories.BarChart(ds)
    val font = new Font("Dialog", Font.PLAIN,5);

    chart.peer.getCategoryPlot.getDomainAxis().
      setCategoryLabelPositions(CategoryLabelPositions.UP_90);
    chart.peer.getCategoryPlot.getDomainAxis.setLabelFont(font)
    chart.show()
  }
}
